using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

 public   class CLSMAESUB : ObservableObject
    {

MDOgeneral mod = new MDOgeneral();

#region Metodos basicos tabla
public List<CLSMAESUB> listaSubg()
    {
        try
        {
            string query = "select CODGRP_SUB,CODSUB_SUB,NOMSUB_SUB from MAESUB where CODGRP_SUB='" + CODGRP_SUB + "' order by NOMSUB_SUB";
                DataTable val = mod.consulta(query);
                List<CLSMAESUB> lisSubg = new List<CLSMAESUB>();
                foreach (DataRow r in val.Rows)
                {
                    lisSubg.Add(new CLSMAESUB { CODGRP_SUB = r["CODGRP_SUB"].ToString().Trim(), CODSUB_SUB = r["CODSUB_SUB"].ToString().Trim(), NOMSUB_SUB = r["NOMSUB_SUB"].ToString().Trim(),EXISTE=true,CAMBIO=false});
                }
                return lisSubg;
        }
        catch (Exception e) { throw e; }
    }
    public int actualizar()
    {
        try
        {
            string query = "update MAESUB set NOMSUB_SUB='" + NOMSUB_SUB + "' where CODGRP_SUB='" + CODGRP_SUB + "' and CODSUB_SUB='" + CODSUB_SUB + "'";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public int insertar()
    {
        try
        {
            string query = "insert into MAESUB (CODGRP_SUB,CODSUB_SUB,NOMSUB_SUB) values ('" + CODGRP_SUB + "','" + CODSUB_SUB + "','"+NOMSUB_SUB+"')";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public void consultar() 
    {
        try
        {
            string query = "select CODGRP_SUB,CODSUB_SUB,NOMSUB_SUB from MAESUB where CODGRP_SUB='" + CODGRP_SUB + "' and CODSUB_SUB='" + CODSUB_SUB + "'";
            DataTable val = mod.consulta(query);
            if (val.Rows.Count > 0)
            {
                this.NOMSUB_SUB = val.Rows[0]["NOMSUB_SUB"].ToString();
                EXISTE = true;
                CAMBIO = false;
            }
            else
            {
                 EXISTE = false;
            }
        }
        catch (Exception e) { throw e; }
    }
    public Boolean existe() {
        try {
            string query = "select count(*) from MAESUB where CODGRP_SUB='" + CODGRP_SUB + "' and CODSUB_SUB='" + CODSUB_SUB + "'";
            DataTable val = mod.consulta(query);
            return val.Rows[0][0].ToString().Equals("0") ? false : true;
        }catch(Exception ex){throw(ex); }
    }
    public Boolean lisExiste(object _lisSub,object _Sub)
    {
        try
        {
            List<CLSMAESUB> lisSub= (List<CLSMAESUB>)_lisSub;
            CLSMAESUB Sub = (CLSMAESUB)_Sub;
            if (lisSub.FindAll(r => r.CODSUB_SUB.Equals(Sub.CODSUB_SUB)).Count > 1)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex) { throw (ex); }
    }
    public int eliminar()
    {
        try
        {
            string query = "delete from MAESUB where CODGRP_SUB='"+CODGRP_SUB+"' and CODSUB_SUB='"+CODSUB_SUB+"'";
            return mod.comando(query);
        }
        catch (Exception e) { throw e; }
    }
    public DataTable buscar()
    {
        try
        {
            string query = "select CODSUB_SUB as Codigo, NOMSUB_SUB as Nombre from MAESUB  where CODGRP_SUB='" + CODGRP_SUB+"' and ";


            if (NOMSUB_SUB != "")
            {
                query = query + "UPPER(NOMSUB_SUB) like '%" + NOMSUB_SUB.ToUpper() + "%' and ";
            }

            query = query.Remove(query.Length - 4).Trim() + " Order by NOMSUB_SUB asc";

            return mod.consulta(query);
        }
        catch (Exception e) { throw e; }
    }
    public Boolean exiMov()
    {
        try
        {
            //Maestro materiales
            string query = "select count(*) from MAEMAT where CODGRP_MAT='"+CODGRP_SUB+"' and CODSUB_MAT ='" + CODSUB_SUB + "'";
            if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
            {
                return true;
            }
            return false;
        }
        catch (Exception ex) { throw (ex); }
    }

#endregion

#region  Propiedades tabla MAECIUP

    private string _CODGRP_SUB;
    public string CODGRP_SUB
    {
        get { return _CODGRP_SUB; }
        set
        {   _CODGRP_SUB = value;
            RaisePropertyChangedEvent("CODGRP_SUB");
        }
    }
    private string _CODSUB_SUB;
    public string CODSUB_SUB
    {
        get { return _CODSUB_SUB; }
        set
        {
            _CODSUB_SUB = value;
            RaisePropertyChangedEvent("CODSUB_SUB");
        }
    }
    private string _NOMSUB_SUB;
    public string NOMSUB_SUB{
        get { return _NOMSUB_SUB; }
        set {
            if (_NOMSUB_SUB != value) { CAMBIO = true; }
            _NOMSUB_SUB = value;
            RaisePropertyChangedEvent("NOMSUB_SUB");
        }
    }
    private Boolean _CAMBIO=false;
    public Boolean CAMBIO
    {
        get { return _CAMBIO; }
        set
        {
            _CAMBIO = value;
            RaisePropertyChangedEvent("CAMBIO");
        }
    }
    private Boolean _EXISTE=false;
    public Boolean EXISTE
    {
        get { return _EXISTE; }
        set
        {
            _EXISTE = value;
            RaisePropertyChangedEvent("EXISTE");
        }
    }
    private Boolean _ELIMINAR = false;
    public Boolean ELIMINAR
    {
        get { return _ELIMINAR; }
        set
        {
            if (_ELIMINAR != value) { CAMBIO = true; }
            _ELIMINAR = value;
            RaisePropertyChangedEvent("ELIMINAR");
        }
    }

#endregion

      }
}
